import openpyxl
import os,configparser,logging,time
import pandas as pd

cfg_file = "cfg.ini"

def log_init():
    if not(os.path.exists("LogFile")):
        os.mkdir("LogFile")
        print("create dir")
    if not(os.path.exists("export")):
        os.mkdir("export")
    time_str = time.strftime('%Y_%m%d_%H%M%S', time.localtime())
    log_file_name = ".\\LogFile\\"+time_str+".txt"
    logging.basicConfig(filename=log_file_name,format="%(asctime)s %(name)s:%(levelname)s-->%(message)s",level='DEBUG',encoding='utf-8')

def check_data(df_bom,df_origin):
    logging.info("Check data start...")
    length = df_bom.shape[0]
    for each in df_bom.index:
        # logging.info("开始查询存货编码:"+str(each))
        ser = df_bom.loc[each]
        standard = df_origin.loc[each]
        res_info = True if (ser["描述"]==standard["存货描述"]) else False
        res_name = True if (ser["名称"]==standard["存货名称"]) else False
        res_partno = True if(ser["出厂型号"]==standard["规格型号"]) else False
        logging.info(each+"--Result:名称:{name},描述:{info},型号:{no}".format(info=res_info,name=res_name,no=res_partno))

        

if __name__=="__main__":
    log_init()
    cfg = configparser.ConfigParser()
    cfg.read(cfg_file,'utf-8')
    data_file = cfg["default"]["data"]
    bom_file = cfg["default"]["bom"]
    logging.info("Read data file:"+data_file)
    logging.info("Read BOM file:"+bom_file)
    df_bom = pd.read_excel(bom_file,header=1,index_col="存货编码",dtype={"存货编码":str})
    df_origin = pd.read_excel(data_file,header=0,index_col="存货编码",dtype={"存货编码":str})
    check_data(df_bom,df_origin)
    df_bom = pd.read_excel(bom_file,header=1,sheet_name="XJ0030-4",index_col="存货编码",dtype={"存货编码":str})
    df_bom = df_bom[df_bom.index==df_bom.index]
    check_data(df_bom,df_origin)
    
